﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Security.Cryptography;
using System.Text;

public class BuildBundle : Editor
{
    [MenuItem("Tool/Build AB")]
    public static void Build()
    {
        //生成bundle包
        BuildAB();

        //拷贝lua文件
        HandleLuaFile();

        //生成file.txt文件
        GenFileText();
    }

    [MenuItem("Tool/Clear Persitent Path")]
    public static void ClearPersitentPath()
    {
        string path = Application.persistentDataPath;

        if (Directory.Exists(path))
            Directory.Delete(path, true);//递归删除目录，子目录和子文件全部删除

        Directory.CreateDirectory(path);
    }

    public static void BuildAB()
    {
        string path = Application.streamingAssetsPath;

        if (Directory.Exists(path))
            Directory.Delete(path, true);//递归删除目录，子目录和子文件全部删除

        //创建路径
        Directory.CreateDirectory(path);

        BuildPipeline.BuildAssetBundles(path, 0, BuildTarget.StandaloneWindows64);
    }

    public static void HandleLuaFile()
    {
        string path = Application.dataPath + "/Lua";
        string[] files = Directory.GetFiles(path, "*.lua");//搜索所有的lua文件

        string desPath = Application.streamingAssetsPath + "/Lua";
        if (!Directory.Exists(desPath))
        {
            Directory.CreateDirectory(desPath);
        }

        for (int i = 0; i < files.Length; i++)
        {
            string fileName = Path.GetFileName(files[i]);
            string des = desPath + "/" + fileName;
            File.Copy(files[i], des);
        }
    }

    static List<string> files = new List<string>();
    static void Recusive(string path)
    {
        string[] fs = Directory.GetFiles(path);
        files.AddRange(fs);

        string[] dires = Directory.GetDirectories(path);
        foreach (var item in dires)
        {
            Recusive(item);
        }
    }

    public static void GenFileText()
    {
        string path = Application.streamingAssetsPath;
        files.Clear();
        Recusive(path);

        StreamWriter writer = File.CreateText(path + "/file.txt");

        foreach (var file in files)
        {
            if (Path.GetExtension(file) != ".meta")//判断后缀
            {
                string md5 = md5file(file);

                string fileName = Path.GetFileName(file);//获取文件名
                writer.WriteLine(fileName + "|" + md5);
            }
        }

        writer.Close();

        //刷新文件夹
        AssetDatabase.Refresh();
    }

    //计算文件的md5
    static string md5file(string file)
    {
        FileStream fs = new FileStream(file, FileMode.Open);
        MD5 md5 = new MD5CryptoServiceProvider();

        byte[] retVal = md5.ComputeHash(fs);
        fs.Close();

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < retVal.Length; i++)
        {
            sb.Append(retVal[i].ToString("x2"));//小写的16进制
        }
        return sb.ToString();
    }
}
